#include "Task.h"

string Task::sendStatusTask(STATUS_TYPE_TASK status) const {
	switch (status){
		case DONE:
			return "Done";
			break;
		case NOT_DONE:
		default:
			return "Not Done";
			break;
		case OVERDUE:
			return "Overdue";
			break;
	}
}
Task::Task(void): _statusCompleted(false)
{
	_entryType = TYPE_ENTRY;
}
Task::Task(string name, QDate endDate, QTime endTime, bool statusCompleted) : Entry(name, endDate, endTime), _statusCompleted(statusCompleted) {
	_entryType= TYPE_ENTRY;
}
Task::Task(const Entry* other): Entry(other->getName(), other->getEndDate(), other->getEndTime()), _statusCompleted(other->getStatus()) {
	_entryType= TYPE_ENTRY;
}
Task::~Task(void)
{
}
string Task::toString() {
	char buffer[1000];
	sprintf_s(buffer, MSG_OUTPUT.c_str(), 
		TYPE_ENTRY.c_str(), getName().c_str(), 
		getEndDate().toString(FORMAT_DATE).toStdString().c_str(),getEndTime().toString(FORMAT_TIME).toStdString().c_str(), _statusCompleted);
	return buffer;
}
Task Task::fromString(string input) {
	istringstream inStream(input);
	string name;
	string endDate;
	string endTime;
	string dump;
	bool statusCompleted;
	getline(inStream, dump, ' ');
	getline(inStream, name);
	getline(inStream, dump, ' ');
	getline(inStream, endDate);
	getline(inStream, dump, ' ');
	getline(inStream, endTime);
	getline(inStream, dump, ' ');
	inStream >> statusCompleted;
	return Task(name, QDate::fromString(QString::fromStdString(endDate), FORMAT_DATE), QTime::fromString(QString::fromStdString(endTime), FORMAT_TIME), statusCompleted);
}
bool Task::operator==(Entry* input) {
	return (getName()==input->getName() &&
		getEndDate()==input->getEndDate() &&
		getEndTime()==input->getEndTime() &&
		getStatus()==input->getStatus() &&
		getEntryType()==input->getEntryType());
}
bool Task::NotDoneOrOverdue () const {
		QDate const cd = QDate::currentDate();
		QTime const ct = QTime::currentTime();
		QDate const taskEndDate = getEndDate();
		QTime const taskEndTime = getEndTime();
		QDateTime const cdt(cd, ct);
		QDateTime const taskDT(taskEndDate,taskEndTime); 
		return((taskDT > cdt) ? 1 : 0);
}

string Task::setNotDoneOrOverdue () const {
	return ((NotDoneOrOverdue()) == true? sendStatusTask(NOT_DONE) : sendStatusTask(OVERDUE));
}

bool Task::setDone() {
	if(_statusCompleted) {
		_statusCompleted = false;
	} else {
		_statusCompleted = true;
	}
	return _statusCompleted;
}
bool Task::getStatus() const {
	return _statusCompleted;
}
string Task::getStatusString() const {
	//return (_statusCompleted==true ? sendStatusTask(DONE) : sendStatusTask(NOT_DONE));
	//return (_statusCompleted==true? "Done" : "Not Done");
	if(_statusCompleted){
		return sendStatusTask(DONE);
	}
	else{
		return setNotDoneOrOverdue();
	}
}
string Task::getEntryType() const {
	return _entryType;
}
int Task::getAttributeCount() const {
	return Task::NUM_ATTRIBUTES;
}
string Task::getSearchString() const{
	char buffer[1000];
	sprintf_s(buffer ,"%s %s %s %s", getName().c_str(), getEndDate().toString("dddd MMMM yyyy dd/MM/yy dd/MM/yyyy dd/M/yy dd/M/yyyy").toStdString().c_str(), getEndTime().toString("h:mm hmmap H:mm Hmm").toStdString().c_str(), getStatusString().c_str());
	return buffer;
}

//void Task::setName(string name) {
//	Entry::setName(name);
//}
const string Task::MSG_OUTPUT= "%s\nNAME: %s\nDUE_DATE: %s\nDUE_TIME: %s\nSTATUS: %d\n";
const string Task::TYPE_ENTRY= "TASK";